package cn.tedu._05weibo.controller;

import cn.tedu._05weibo.commen.response.JsonResult;
import cn.tedu._05weibo.commen.response.StatusCode;
import cn.tedu._05weibo.mapper.UserMapper;
import cn.tedu._05weibo.pojo.dto.UserLoginDTO;
import cn.tedu._05weibo.pojo.dto.UserRegDTO;
import cn.tedu._05weibo.pojo.entity.User;
import cn.tedu._05weibo.pojo.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;

@Api(tags = "用户模块")
@Slf4j
@RestController
@RequestMapping("/v1/users")
public class UserController {
    @Autowired
    private UserMapper userMapper;

    /**
     * 用户注册功能
     * 接口地址: /v1/users/reg
     * 请求方式: POST
     * 请求参数类型: application/json
     * 返回响应: int
     */
    @ApiOperation(value = "1.用户注册")
    @PostMapping("/reg")
    //使用Content-Type: application/json需要添加@RequestBody注解, 否则服务端接收不到数据
    public JsonResult reg(@RequestBody @Validated UserRegDTO userRegDTO) {
        /*
            1.数据合法性校验;
            2.验证用户名是否被占用[查询接口];
              SELECT ... FROM user WHERE username=用户名
              2.1 被占用: return 2;
              2.2 未被占用: 插入数据表[插入接口], return 1;
         */
        //打桩输出 soutp(打印参数列表)
        log.debug("userRegDTO = " + userRegDTO);
        //确认用户名是否被占用
        UserVO userVO = userMapper.selectByUsername(userRegDTO.getUsername());
        if (userVO != null) { //被占用
            return new JsonResult(StatusCode.USERNAME_ALREADY_EXISTS);
        }
        //存入数据
        User user = new User();
        BeanUtils.copyProperties(userRegDTO, user);
        userMapper.insertUser(user);
        return JsonResult.success();
    }

    /**
     * 用户登录功能
     * 接口地址: /v1/users/login
     * 请求方式: POST
     * 请求数据类型: application/json
     * 响应数据类型: int, 1:登陆成功, 2:密码错误, 3:用户名错误
     */
    @ApiOperation(value = "2.用户登录")
    @PostMapping("/login")
    public JsonResult login(@RequestBody UserLoginDTO userLoginDTO,
                            @ApiIgnore HttpSession session) {
        //打桩输出
        log.debug("userLoginDTO = " + userLoginDTO);
        //查询验证用户名和密码
        UserVO userVO = userMapper.selectByUsername(userLoginDTO.getUsername());
        //用户名错误
        if (userVO == null)
            return new JsonResult(StatusCode.USERNAME_ERROR);
        //密码错误
        if (!userVO.getPassword().equals(userLoginDTO.getPassword()))
            return new JsonResult(StatusCode.PASSWORD_ERROR);
        //登陆成功
        session.setAttribute("user", userVO);
        return new JsonResult(StatusCode.LOGIN_SUCCESS);
    }

    /**
     * 获取当前用户功能
     * 接口地址: /v1/users/currentUser
     * 请求方式: GET
     * 请求数据类型: application/x-www-form-urlencoded
     * 响应数据类型: userVo
     */
    @ApiOperation(value = "3.获取当前用户")
    @GetMapping("/currentUser")
    public JsonResult currentUser(@ApiIgnore HttpSession session) {
        UserVO userVO = (UserVO) session.getAttribute("user");
        return JsonResult.success(userVO);
    }

    /**
     * 退出登录功能
     * 请求地址: /v1/users/logout
     * 请求方式: GET
     * 请求参数: 无
     * 相应参数: 无
     */
    @ApiOperation(value = "4.用户退出")
    @GetMapping("/logout")
    public JsonResult logout(@ApiIgnore HttpSession session) {
        session.removeAttribute("user");
        log.debug("用户已登出");
        return JsonResult.success();
    }
}
